
$(document).ready(function () {
    showInfo();

    var isFoucs =$('input:radio[name="isFocusUpdate"]:checked').val();
    console.log(isFoucs);

    if(isFoucs == 'YES')  {
        disableFocusUpdate();
    }

    else {
        enableFocusUpdate();
    }


    $('input[type=radio][name=isFocusUpdate]').change(function() {
        if (this.value == 'YES') {
            disableFocusUpdate();
        }
        else {
            enableFocusUpdate();
        }
    });

});

function disableFocusUpdate() {
    $("#focusUpdateSpan1").css("display", "table-row");
    $("#focusUpdateSpan2").css("display", "table-row");
    $('#focusUpdateMin').attr("nullmsg", "不能为空！");
    $('#focusUpdateMin').attr("errormsg", "不能为空！");
    $('#focusUpdateMin').attr("datatype", "*");
    $('#focusUpdateMax').attr("nullmsg", "不能为空！");
    $('#focusUpdateMax').attr("errormsg", "不能为空！");
    $('#focusUpdateMax').attr("datatype", "*");
}

function enableFocusUpdate() {
    $("#focusUpdateSpan1").css("display", "none");
    $("#focusUpdateSpan2").css("display", "none");
    $('#focusUpdateMin').removeAttr("nullmsg");
    $('#focusUpdateMin').removeAttr("errormsg");
    $('#focusUpdateMin').removeAttr("datatype");
    $('#focusUpdateMax').removeAttr("nullmsg");
    $('#focusUpdateMax').removeAttr("errormsg");
    $('#focusUpdateMax').removeAttr("datatype");
}

var comValidform = null;
var returnStatus = false;

function showInfo() {
    var id = art.dialog.data('id');
    var key = id + "_ProductVer_Key";

    var appVersion = eval('(' + localStorage.getItem(key) + ')');

    $("input[name='id']").val(id);     //资源ID
    $("input[name='userId']").val(userId);
    $("input[name='channel']").val(appVersion.channel);
    $("input[name='channelText']").val(appVersion.channelText);
    $("input[name='phoneType']").val(appVersion.phoneType);
    $("input[name='appVersion']").val(appVersion.version); //版本号
    $("input[name='name']").val(appVersion.name);      //版本名称

    var forceUpdate = appVersion.isFocusUpdate;      //强制更新
    //console.log("forceUpdate:"+forceUpdate);

    if (forceUpdate == 'YES') {
        $('#forceUpdate0').attr('checked', 'true');
    } else if (forceUpdate == 'NO') {
        $('#forceUpdate1').attr('checked', 'true');
    }

    //是否强制更新最低版本
    $("input[name='focusUpdateMin']").val(appVersion.focusUpdateMin);
    //是否强制更新最高版本
    $("input[name='focusUpdateMax']").val(appVersion.focusUpdateMax);

    //版本描述
    $("textarea[name='description']").val(appVersion.description);

}

function submitForm(callback) {
    comValidform = $(".com-addForm").Validform({
        tiptype: 2,
        tipSweep: true,
        beforeSubmit: function (curform) {
            var formData = new FormData($("#com-addForm")[0]);

            $.ajax({
                url: APP_ADDRESS + "/app/version/update",
                type: "POST",
                data: formData,
                enctype: 'multipart/form-data',
                processData: false,
                contentType: false,
                cache: false,
                success: function (data) {
                    console.log(data);
                    //成功返回
                    if (data.resultCode == "200") {
                        //关闭窗口并刷新数据
                        callback();
                        art.dialog.close();
                    }
                    else
                        alert(data.result);
                },
                error: function (data) {
                    console.log(data);
                }
            });

        }
    });

    comValidform.submitForm();
    return returnStatus;
}
